package app.itgungnir.compose.wan.business

import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.core.view.WindowCompat
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import app.itgungnir.compose.wan.Routes
import app.itgungnir.compose.wan.business.coin_manifest.CoinManifestScreen
import app.itgungnir.compose.wan.business.coin_rank.CoinRankScreen
import app.itgungnir.compose.wan.business.login.LoginScreen
import app.itgungnir.compose.wan.business.main.MainScreen
import app.itgungnir.compose.wan.business.message.MessageScreen
import app.itgungnir.compose.wan.business.my_collection.MyCollectionScreen
import app.itgungnir.compose.wan.business.my_website.MyWebsiteScreen
import app.itgungnir.compose.wan.business.register.RegisterScreen
import app.itgungnir.compose.wan.business.settings.SettingsScreen
import app.itgungnir.compose.wan.business.splash.SplashScreen
import app.itgungnir.compose.wan.business.user_square.UserSquareScreen
import app.itgungnir.compose.wan.theme.WanComposeTheme
import com.google.accompanist.insets.ProvideWindowInsets
import com.google.accompanist.systemuicontroller.rememberSystemUiController

class WanActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        WindowCompat.setDecorFitsSystemWindows(window, false)
        setContent {
            val systemUiController = rememberSystemUiController()
            SideEffect {
                systemUiController.setStatusBarColor(color = Color.Transparent, darkIcons = false)
                systemUiController.setNavigationBarColor(color = Color.Transparent, darkIcons = true)
            }
            WanComposeTheme {
                ProvideWindowInsets {
                    ScreenHost(this)
                }
            }
        }
    }

    @Composable
    private fun ScreenHost(activity: AppCompatActivity) {
        val navController = rememberNavController()
        NavHost(modifier = Modifier.fillMaxSize(), navController = navController, startDestination = Routes.SPLASH) {
            composable(route = Routes.SPLASH) { SplashScreen(navController = navController) }
            composable(route = Routes.MAIN) { MainScreen(navController = navController, activity = activity) }
            composable(route = Routes.LOGIN) { LoginScreen(navController = navController) }
            composable(route = Routes.REGISTER) { RegisterScreen(navController = navController) }
            composable(route = Routes.COIN_MANIFEST) { CoinManifestScreen(navController = navController) }
            composable(route = Routes.COIN_RANK) { CoinRankScreen(navController = navController) }
            composable(route = Routes.MESSAGE) { MessageScreen(navController = navController) }
            composable(route = Routes.SETTINGS) { SettingsScreen(navController = navController) }
            composable(route = Routes.MY_COLLECTIONS) { MyCollectionScreen(navController = navController) }
            composable(route = Routes.MY_WEBSITES) { MyWebsiteScreen(navController = navController) }
            composable(
                route = Routes.UserSquare.FULL_ROUTE,
                arguments = listOf(
                    navArgument(name = Routes.UserSquare.ARG_USER_ID) { type = NavType.LongType },
                    navArgument(name = Routes.UserSquare.ARG_USER_NAME) { type = NavType.StringType }
                )
            ) { entry ->
                val userId = entry.arguments?.getLong(Routes.UserSquare.ARG_USER_ID) ?: 0L
                val userName = entry.arguments?.getString(Routes.UserSquare.ARG_USER_NAME) ?: ""
                UserSquareScreen(navController = navController, userId = userId, userName = userName)
            }
        }
    }
}
